package com.gopay.bis.cmb.ebank.impl;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.bis.cmb.ebank.operation.CmbB2bCerthelper;
import com.gopay.bis.cmb.ebank.operation.CmbB2bQueryHelper;
import com.gopay.bis.cmb.ebank.operation.HttpTransport;
import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.cmb.ebank.pay.CmbB2bMessage;
import com.gopay.common.domain.bank.cmb.ebank.pay.CmbB2bPayResult;
import com.gopay.common.domain.bank.cmb.ebank.query.CmbB2bQueryReq;
import com.gopay.common.domain.bank.cmb.ebank.query.CmbB2bQueryRes;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.businesses.BankService;

/** 
 * @ClassName: CmbB2bBankServiceImpl 
 * @Description: 招商银行b2b处理类 
 * @author w_jiqing 
 * @date 2016年8月29日 下午2:01:52  
 */ 
public class CmbB2bBankServiceImpl implements BankService{
    
    private static Logger logger = LoggerFactory.getLogger(CmbB2bBankServiceImpl.class);

    private CmbB2bCerthelper cmbB2bCerthelper;
    private CmbB2bQueryHelper cmbB2bQueryHelper;
    
    private HttpTransport httpTransport;
    
    private int timeout = 15000;
    
    private String url;

    private static String channles ="http://211.88.23.21:10000/";
    private List<String> urls;
    private Map<String, String> logNames;

    @Override
    public Object sign(BaseMessage msg) throws GopayException {
        CmbB2bMessage message =(CmbB2bMessage) msg;
        message.setSignMessage(cmbB2bCerthelper.sign(message));
        return message;
    }

    @Override
    public Object verify(BaseMessage msg) throws GopayException {
        CmbB2bPayResult message =(CmbB2bPayResult) msg;        
        return cmbB2bCerthelper.verify(message);
    }

    @Override
    public Object query(Object obj) throws GopayException {
        CmbB2bQueryRes resMessage= new CmbB2bQueryRes();
        String another = "";
        try{
            CmbB2bQueryReq message = (CmbB2bQueryReq) obj;            
            String url = "";
            String name = "";            
            for(String str : urls){
                if(channles.startsWith(str)){
                    name = logNames.get(str);
                    url = str;
                } else{
                    another = str;
                }
            }
            message.setPlainMessage(cmbB2bQueryHelper.buildQueryXml(message, name));
            logger.info("cmb b2b 查询请求对象："+ToStringBuilder.reflectionToString(message));
            httpTransport.setUrl(url);
            httpTransport.setTimeout(timeout);
            Object res= httpTransport.submit(message.getPlainMessage().getBytes("GBK"));
            resMessage = cmbB2bQueryHelper.parseQueryXml(res);
            if("-1".equals(resMessage.getInfo().getRetcod())){
//                channles=another;
                throw new GopayException(ProcCodeConstants.PROC_CODE_100E6036,"请求银行失败，链接前置机"+url);
            }
        } catch(IOException e){
//            channles=another;
            throw new GopayException(ProcCodeConstants.PROC_CODE_100E6036,"切换前置机为："+channles);           
        } catch(Exception ex){
            BankLogUtils.printB2BQueryExceptionLog(resMessage,ex);
            throw new GopayException(ProcCodeConstants.PROC_CODE_100E6032);
        }
        return resMessage;
    }

    @Override
    public Object dayend(Object obj) throws GopayException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Object refund(Object obj) throws GopayException {
        // TODO Auto-generated method stub
        return null;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public CmbB2bCerthelper getCmbB2bCerthelper() {
        return cmbB2bCerthelper;
    }

    public void setCmbB2bCerthelper(CmbB2bCerthelper cmbB2bCerthelper) {
        this.cmbB2bCerthelper = cmbB2bCerthelper;
    }

    public HttpTransport getHttpTransport() {
        return httpTransport;
    }

    public void setHttpTransport(HttpTransport httpTransport) {
        this.httpTransport = httpTransport;
    }


    public static String getChannles() {
        return channles;
    }

    public static void setChannles(String channles) {
        CmbB2bBankServiceImpl.channles = channles;
    }

    public List<String> getUrls() {
        return urls;
    }

    public void setUrls(List<String> urls) {
        this.urls = urls;
    }

    public Map<String, String> getLogNames() {
        return logNames;
    }

    public void setLogNames(Map<String, String> logNames) {
        this.logNames = logNames;
    }

    public CmbB2bQueryHelper getCmbB2bQueryHelper() {
        return cmbB2bQueryHelper;
    }

    public void setCmbB2bQueryHelper(CmbB2bQueryHelper cmbB2bQueryHelper) {
        this.cmbB2bQueryHelper = cmbB2bQueryHelper;
    }

}
